package com.tycmc.signelectroniccontracts.logic.renzheng;

import android.content.Intent;
import android.view.View;

import com.meiya.cunnar.opencloud.livingcertificationkit.LivingCertificationHelper;
import com.meiya.cunnar.opencloud.livingcertificationkit.VerifyCallback;
import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.act.htong.ActHtList;
import com.tycmc.signelectroniccontracts.act.renzheng.ActFirstName;
import com.tycmc.signelectroniccontracts.act.renzheng.ActPeopleRenzhen;
import com.tycmc.signelectroniccontracts.tools.AppData;
import com.tycmc.signelectroniccontracts.tools.base.LogicParent;
import com.tycmc.signelectroniccontracts.tools.manager.AppManager;
import com.tycmc.signelectroniccontracts.tools.net.NetUrl;
import com.tycmc.signelectroniccontracts.tools.net.NetWorkUtils;
import com.tycmc.signelectroniccontracts.tools.net.RequestAction;
import com.tycmc.signelectroniccontracts.tools.net.result.NetworkResult;
import com.tycmc.signelectroniccontracts.tools.net.result.ProGressUpDataListen;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.show.AlertDialog;
import com.tycmc.signelectroniccontracts.tools.show.LogShow;
import com.tycmc.signelectroniccontracts.tools.show.ProDialog;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.store.FileStore;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;

import java.io.File;
import java.util.Map;

public class PreFirstName extends LogicParent {

    private FirstNameInterface pre;
    private ActFirstName context;

    public PreFirstName(FirstNameInterface pre) {
        this.pre = pre;
        context = ((ActFirstName) pre);
        pre.renzhenStyle(SharePre.getString(SharePreName.clientTypeID, "").equals("01"));
        pre.initData(SharePre.getString(SharePreName.modile, ""), context.getString(R.string.no_renzheng));
    }

    public void startGeren() {
        getInfo();

    }

    public void getInfo() {
        RequestAction action = new RequestAction(NetUrl.getCompanyInfo);
        action.putBody("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
        action.putBody("clientTypeID", SharePre.getString(SharePreName.clientTypeID, ""));
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).postJson(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {

                pre.sendRenlianshibie(TextShow.valueOf(data.get("clientName")), TextShow.valueOf(data.get("idcNo")));
                dialog.dismiss();
            }

            @Override
            public void failed(Object data) {
                context.start(new Intent(context, ActPeopleRenzhen.class));
                dialog.dismiss();
            }
        });
    }

    public void huotijiance(final String name, final String idcard) {
        RequestAction action = new RequestAction(NetUrl.getCunnarToken);
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).postJson(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                huoti(name, idcard, TextShow.valueOf(data.get("token")));
            }

            @Override
            public void failed(Object data) {
                ToastShow.popupToast(data + "");
                new AlertDialog(context).builder().setTitle("温馨提示").setMsg("认证失败需要重新进行认证").setPositiveButton("认证", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pre.sendRenlianshibie(name, idcard);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                }).show();
                dialog.dismiss();
            }
        });

    }

    public void huoti(final String name, final String idcard, String accessToken) {
        LivingCertificationHelper
                .verify(context, /*此处接收姓名*/name, /*此处接收身份证号*/idcard, /*此处接收AccessToken*/accessToken, /*此处接收AppKey*/AppData.appkey,
                        new VerifyCallback() {
                            @Override
                            public void onVerifyCompleted(boolean verify, String msg, byte[] picResult) {
                                LogShow.i("msg", msg + "");
                                if (verify) {
                                    RequestAction action = new RequestAction(NetUrl.savePersonaalFace);
                                    action.putBody("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
                                    action.putBody("clientTypeID", SharePre.getString(SharePreName.clientTypeID, ""));
                                    if (null != picResult) {
                                        String save = FileStore.save(System.currentTimeMillis() + ".png", picResult);
                                        File[] files = new File[1];
                                        files[0] = new File(save);
                                        action.putBody("multipartFiles", files);
                                    }
                                    final ProDialog dialog = new ProDialog(context, "");
                                    dialog.show();
                                    new NetWorkUtils(context).upLoadProgress(action, new ProGressUpDataListen() {
                                        @Override
                                        public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {

                                        }

                                        @Override
                                        public void sucess(Map<String, Object> data, String str) {
                                            dialog.dismiss();

                                            context.start(new Intent(context, ActHtList.class));
                                            AppManager.getAppManager().finishAllActivity();
                                        }

                                        @Override
                                        public void failed(Object data) {
                                            ToastShow.popupToast(data + "");

                                            dialog.dismiss();
                                            new AlertDialog(context).builder().setTitle("温馨提示").setMsg("认证失败需要重新进行认证").setPositiveButton("认证", new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    pre.sendRenlianshibie(name, idcard);
                                                }
                                            }).setNegativeButton("取消", new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    context.exit();
                                                }
                                            }).show();
                                        }
                                    });

                                } else {
                                    ToastShow.popupToast(msg);
                                    new AlertDialog(context).builder().setTitle("温馨提示").setMsg("认证失败需要重新进行认证").setPositiveButton("认证", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            pre.sendRenlianshibie(name, idcard);
                                        }
                                    }).setNegativeButton("取消", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            context.exit();
                                        }
                                    }).show();
                                }

                            }
                        });
    }

    public interface FirstNameInterface {
        void renzhenStyle(boolean name);//显示认证方式 true 个人

        void initData(String modile, String modiletype);//认证手机号 认证状态

        void sendRenlianshibie(String clientName, String idcNo);
    }
}
